package com.market.cash.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.market.cash.R
import com.market.cash.databinding.CashBindCardSubmitBinding
import com.market.cash.pro.ProDialog
import com.market.cash.utils.CashRootImageUtil
import com.market.cash.utils.adapter.CashRootOnItemChildClickListener
import com.market.cash.utils.adapter.CashRootRecyclerViewAdapter
import com.market.cash.utils.adapter.CashRootViewHolderHelper
import com.market.cash.utils.event.CashRootEventHelper
import com.market.cash.utils.event.CashRootEventType
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/23 13:45
 * @Annotation：绑卡确认dialog
 */
class CashRootBindCardSubmitDialog: ProDialog {
    private lateinit var binding: CashBindCardSubmitBinding
    private var mCallback: IDialogCallback? = null

    private var itemAdapter: ItemAdapter? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = CashBindCardSubmitBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        initRecyclerView()
    }

    override fun initListener() {
        setOnClickListener(binding.imgClose)
        setOnClickListener(binding.btnConfirm)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    /**
     * 初始化账户列表
     */
    private fun initRecyclerView() {
//        val topDecoration = CashRootDensityUtil.dp2px(14f)
//        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0, topDecoration, 0))
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        itemAdapter = ItemAdapter(binding.recyclerView)
        binding.recyclerView.adapter = itemAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        itemAdapter!!.setOnItemChildClickListener(CashRootOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户列表
     */
    fun setBindAccountList(accountList: MutableList<JSONObject>){
        itemAdapter!!.data = accountList
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    class ItemAdapter(recyclerView: RecyclerView?) : CashRootRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.cash_account_submit_item) {
        override fun setItemChildListener(helper: CashRootViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }
        override fun fillData(helper: CashRootViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.textTitle,model.optString("walletDesc"))
            helper.setText(R.id.textMobile,"+" + model.optString("walletMobile"))
            val imgLogo = helper.getImageView(R.id.imgLogo)
            CashRootImageUtil.instance.loadImageUrl(mContext,imgLogo,model.optString("walletLogo"), R.mipmap.cash_holder)
        }
    }

    override fun onStart() {
        super.onStart()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_DISMISS)
    }
}